package com.demo.zhou.sniper.activity;

import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.demo.zhou.sniper.R;
import com.demo.zhou.sniper.dbmanger.SPManager;
import com.demo.zhou.sniper.util.MacUtils;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    private EditText main_et_uid;
    private Button main_bt_login;
    private String password;
    final String str_mac = "4C:49:E3:E7:2D:57";

    //    final String str_mac = "4C:49:E3:E3:B8:77";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        into();
        String strMa = MacUtils.getMobileMAC(getApplicationContext());
        if (MacUtils.ERROR_MAC_STR.equals(strMa)) {
            Toast.makeText(this, "请授予开启wifi权限 以保证正常获取mac", Toast.LENGTH_SHORT).show();
            MacUtils.getStartWifiEnabled();
        }
//        if (!getMAC(str_mac)) {
//            finish();
//        }

    }

    private void into() {
        main_et_uid = findViewById(R.id.main_et_uid);
        main_bt_login = findViewById(R.id.main_bt_login);
        main_bt_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                loginTo();
//                return;

                if (main_et_uid.getText().toString().equals("")) {
                    Toast.makeText(MainActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                } else if (main_et_uid.getText().toString().equals("1234143")) {
                        //权限5
                    main_et_uid.setText("");
                    int loginNum =SPManager.getInstance().getInt("loginNum", 0);
                   if(loginNum<50){
                       SPManager.getInstance().putInt("Jurisdiction", 5);
                       SPManager.getInstance().putInt("loginNum", ++loginNum);
                       loginTo();
                   }else {
                       Toast.makeText(MainActivity.this, "登陆次数不足", Toast.LENGTH_SHORT).show();
                   }

                } else {
                    password = main_et_uid.getText().toString().trim();
                    if (password.equals(getTime())) {
                        //权限1
                        main_et_uid.setText("");
                        SPManager.getInstance().putInt("Jurisdiction", 1);
                        SPManager.getInstance().putInt("loginNum", 0);
                        loginTo();
                    } else if (password.equals(getTime1())) {
                        //权限3
                        main_et_uid.setText("");
                        int loginNum =SPManager.getInstance().getInt("loginNum", 0);
                        if (loginNum<50){
                            SPManager.getInstance().putInt("Jurisdiction", 3);
                            SPManager.getInstance().putInt("loginNum", ++loginNum);
                            loginTo();
                        }else {
                            Toast.makeText(MainActivity.this, "登陆次数不足", Toast.LENGTH_SHORT).show();
                        }

                    } else {
                        Toast.makeText(MainActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
                    }

                }

            }
        });
    }

    private void loginTo() {
        Intent intent = new Intent(MainActivity.this, ChoiceActivity.class);
        startActivity(intent);
    }


    public String getTime1() {
        String password = null;
        Calendar now = Calendar.getInstance();
        String month; //7月为单  8
        if ((now.get(Calendar.MONTH) + 1) % 2 == 1) {
            //为单
            if ((now.get(Calendar.MONTH) + 1) * 8 < 10) {
                month = "0" + (now.get(Calendar.MONTH) + 1) * 8;
            } else {
                month = "" + (now.get(Calendar.MONTH) + 1) * 8;
            }

        } else {
            //为双
            if ((now.get(Calendar.MONTH) + 1) * 5 < 10) {
                month = "0" + (now.get(Calendar.MONTH) + 1) * 5;
            } else {
                month = "" + (now.get(Calendar.MONTH) + 1) * 5;
            }

        }
        String day;//13为单 39
        if (now.get(Calendar.DAY_OF_MONTH) % 2 == 1) {
            if (now.get(Calendar.DAY_OF_MONTH) * 3 < 10) {
                day = "0" + now.get(Calendar.DAY_OF_MONTH) * 3;
            } else {
                day = "" + now.get(Calendar.DAY_OF_MONTH) * 3;
            }

        } else {
            if (now.get(Calendar.DAY_OF_MONTH) / 2 < 10) {
                day = "0" + now.get(Calendar.DAY_OF_MONTH) / 2;
            } else {
                day = "" + now.get(Calendar.DAY_OF_MONTH) / 2;
            }

        }
        String hour;
        if (now.get(Calendar.HOUR_OF_DAY) % 2 == 1) {
            if (now.get(Calendar.HOUR_OF_DAY) - 1 < 10) {
                hour = "0" + (now.get(Calendar.HOUR_OF_DAY) - 1);
            } else {
                hour = "" + (now.get(Calendar.HOUR_OF_DAY) - 1);
            }

        } else {
            if (now.get(Calendar.HOUR_OF_DAY) * 3 < 10) {
                hour = "0" + now.get(Calendar.HOUR_OF_DAY) * 3;
            } else {
                hour = "" + now.get(Calendar.HOUR_OF_DAY) * 3;
            }

        }
        //2030839361
        password = month + day + hour + 2;
        return password;
    }

    public String getTime() {
        String password = null;
        Calendar now = Calendar.getInstance();
        String year = now.get(Calendar.YEAR) + 13 + "";//2030
        String month; //7月为单  8
        if ((now.get(Calendar.MONTH) + 1) % 2 == 1) {
            //为单
            if ((now.get(Calendar.MONTH) + 1) * 8 < 10) {
                month = "0" + (now.get(Calendar.MONTH) + 1) * 8;
            } else {
                month = "" + (now.get(Calendar.MONTH) + 1) * 8;
            }

        } else {
            //为双
            if ((now.get(Calendar.MONTH) + 1) * 5 < 10) {
                month = "0" + (now.get(Calendar.MONTH) + 1) * 5;
            } else {
                month = "" + (now.get(Calendar.MONTH) + 1) * 5;
            }

        }
        String day;//13为单 39
        if (now.get(Calendar.DAY_OF_MONTH) % 2 == 1) {
            if (now.get(Calendar.DAY_OF_MONTH) * 3 < 10) {
                day = "0" + now.get(Calendar.DAY_OF_MONTH) * 3;
            } else {
                day = "" + now.get(Calendar.DAY_OF_MONTH) * 3;
            }

        } else {
            if (now.get(Calendar.DAY_OF_MONTH) / 2 < 10) {
                day = "0" + now.get(Calendar.DAY_OF_MONTH) / 2;
            } else {
                day = "" + now.get(Calendar.DAY_OF_MONTH) / 2;
            }

        }
        String hour;
        if (now.get(Calendar.HOUR_OF_DAY) % 2 == 1) {
            if (now.get(Calendar.HOUR_OF_DAY) - 1 < 10) {
                hour = "0" + (now.get(Calendar.HOUR_OF_DAY) - 1);
            } else {
                hour = "" + (now.get(Calendar.HOUR_OF_DAY) - 1);
            }

        } else {
            if (now.get(Calendar.HOUR_OF_DAY) * 3 < 10) {
                hour = "0" + now.get(Calendar.HOUR_OF_DAY) * 3;
            } else {
                hour = "" + now.get(Calendar.HOUR_OF_DAY) * 3;
            }

        }
        //2030839361
        password = year + month + day + hour + 2;
        return password;
    }

    private boolean getMAC(String str) {
        if (MacUtils.getMobileMAC(this).equals(str)) {
            return true;
        }
        return false;
    }
}
